#include <iostream>
#include <memory>
#include <string>
#include <thread>
#include <grpcpp/grpcpp.h>
#include "../protos/ImageDetection.grpc.pb.h"
#include "../protos/ImageDetection.pb.h"
#include "../atomic_include/ImageDetection.h"

class ImageDetection_Service_Client {
public:
    ImageDetection_Service_Client(std::shared_ptr<::grpc::Channel> channel)
        : stub_(::ImageDetection_Package::ImageDetection_Service::NewStub(channel)){}
    

    DetectionResult imageDetect(const RequestSignal& ImageDetection_Request_t){
        ImageDetection_Package::RequestSignal request;
        
        
        request.set_signal(ImageDetection_Request_t.signal);
        
        
        //Received reply data translate to ImageDetection_Reply_t; 
        DetectionResult ImageDetection_Reply_t;
        ImageDetection_Package::DetectionResult reply;
        ::grpc::ClientContext context;
        ::grpc::Status status = stub_->imageDetect(&context, request, &reply);
        
        
        ImageDetection_Reply_t.camera_id = reply.camera_id();
        
        
        
        ImageDetection_Reply_t.object_class = reply.object_class();
        
        
        
        ImageDetection_Reply_t.confidence = reply.confidence();
        
        
        
        ImageDetection_Reply_t.x_min = reply.x_min();
        
        
        
        ImageDetection_Reply_t.y_min = reply.y_min();
        
        
        
        ImageDetection_Reply_t.x_max = reply.x_max();
        
        
        
        ImageDetection_Reply_t.y_max = reply.y_max();
        
        
        
        ImageDetection_Reply_t.return_type = reply.return_type();
        
        
        if (status.ok()) {
            std::cout << "ImageDetection--> imageDetect : Received server data success " << std::endl;
        } else {
            std::cout << "ImageDetection--> imageDetect :" << status.error_code() << ": " << status.error_message() << std::endl;
        }
        return ImageDetection_Reply_t;
    }

    

    CameraStatus statusDetect(const RequestSignal& ImageDetection_Request_t){
        ImageDetection_Package::RequestSignal request;
        
        
        request.set_signal(ImageDetection_Request_t.signal);
        
        
        //Received reply data translate to ImageDetection_Reply_t; 
        CameraStatus ImageDetection_Reply_t;
        ImageDetection_Package::CameraStatus reply;
        ::grpc::ClientContext context;
        ::grpc::Status status = stub_->statusDetect(&context, request, &reply);
        
        
        ImageDetection_Reply_t.status_code = reply.status_code();
        
        
        
        ImageDetection_Reply_t.description = reply.description();
        
        
        
        ImageDetection_Reply_t.return_type = reply.return_type();
        
        
        if (status.ok()) {
            std::cout << "ImageDetection--> statusDetect : Received server data success " << std::endl;
        } else {
            std::cout << "ImageDetection--> statusDetect :" << status.error_code() << ": " << status.error_message() << std::endl;
        }
        return ImageDetection_Reply_t;
    }

    
private:
    std::unique_ptr<::ImageDetection_Package::ImageDetection_Service::Stub> stub_;
};